package com.qunar.homework2.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.util.concurrent.Callable;

@Getter
@Setter
@ToString
public class ScrapyTask<T> implements Callable {
    //@Resource
    private static OkHttpClient ohc = new OkHttpClient();

    private String url;

    public ScrapyTask(String url){
        this.url = url;
    }

    @Override
    public T call() throws Exception {
        return process(url);
    }
    private T process(String url) throws Exception{
        Request request = new Request.Builder().get().url(url).build();
        Response response = ohc.newCall(request).execute();
        String json = response.body().string();
        System.out.println(json);
        return JSON.parseObject(json, new TypeReference<T>(){});
    }
}
